<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.conn.RecordSetDataSource" %>
<%@ page import="weaver.general.TimeUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.UUID" %>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs1 = new RecordSet();
    String currenttime = TimeUtil.getCurrentTimeString();
    RecordSetDataSource rsds = new RecordSetDataSource("ehrdb");
    rs.executeQuery("select t.* from formtable_main_770 t join workflow_requestbase t1 on t.REQUESTID=t1.REQUESTID\n" +
            "where CURRENTNODETYPE=3");
    String[] columnName = rs.getColumnName();
    int counts = rs.getCounts();
    int succcount = 0;
    int failcount = 0;
    JSONArray dataList = new JSONArray();
    while (rs.next()) {
        String requestid = Util.null2String(rs.getString("requestid"));
        JSONObject jsonObj = new JSONObject();
        Map mainTableDataMap = new HashMap();
        for (String column : columnName) {
            column = column.toLowerCase();
            String columnValue = Util.null2String(rs.getString(column));
            mainTableDataMap.put(column, columnValue);
        }
        int formid = -770;
        boolean flag = true;
        try {
            String pk_away_h = genEHRPK(requestid);//出差单主表主键
            String fun_code = "6017040410";//审批流节点号
            String applydate = Util.null2String((String) mainTableDataMap.get("ccrq"));//出差日期
            String ishrssbill = "N";//是否员工自助增加
            String isappbill = "N";//是否申请单
            String ccrbh = Util.null2String((String) mainTableDataMap.get("ccrbh"));//出差人工号
            String applier = "";//申请人PK
            String applydept = "";//申请人部门PK
            String pk_corp = "";//申请人公司PK
            String pk_tbm_psndoc = "";//考勤档案主键
            rsds.execute("select a.pk_psndoc,a.pk_psnbasdoc,a.psncode,a.pk_deptdoc,a.pk_corp,b.pk_psndoc bpk_psndoc,b.pk_tbm_psndoc,b.begindate,b.enddate from bd_psndoc a ,tbm_psndoc b where a.pk_psndoc=b.pk_psndoc and a.psnclscope='0' and  a.psncode='" + ccrbh + "' and b.begindate<='" + applydate + "' AND (b.enddate>='" + applydate + "' OR b.enddate='9999-01-01')");
            if (rsds.next()) {
                applier = Util.null2String(rsds.getString("pk_psndoc"));
                applydept = Util.null2String(rsds.getString("pk_deptdoc"));
                pk_corp = Util.null2String(rsds.getString("pk_corp"));
                pk_tbm_psndoc = Util.null2String(rsds.getString("pk_tbm_psndoc"));
            }

            String approvestatus = "200";//审批状态

            String awaytype = Util.null2String((String) mainTableDataMap.get("awaytype"));//请假类型
            rs1.execute("select selectname from workflow_selectitem where fieldid=(select id from workflow_billfield where billid='" + formid + "' and upper(fieldname)='AWAYTYPE') and selectvalue='" + awaytype + "'");
            if (rs1.next()) {
                awaytype = rs1.getString("selectname");
            }

            //请假类型PK
            rsds.execute("select pk_timeitem from tbm_timeitem where fathertimeitem='awayclass' and pk_corp='" + pk_corp + "' and timeitemname='" + awaytype + "'");
            rsds.next();
            awaytype = Util.null2String(rsds.getString("pk_timeitem"));
            String sql = "insert into tbm_awayh (" +
                    "awaytype,approvestatus,applydept,applydate,billname,awayid,pk_corp," +
                    "pk_away_h,fun_code,ishrssbill,isappbill,applier,ts" +
                    ") values (" +
                    "'" + awaytype + "','" + approvestatus + "','" + applydept + "','" + applydate + "','" + pk_away_h + "','" + pk_away_h + "','" + pk_corp + "'," +
                    "'" + pk_away_h + "','" + fun_code + "','" + ishrssbill + "','" + isappbill + "','" + applier + "','" + currenttime + "'" +
                    ")";
            flag = rsds.execute(sql);

            if (flag) {
                String awayoffstatus = "2";//销假状态
                String awaysumhour = Util.null2String((String) mainTableDataMap.get("ccts"));//出差合计工时
                if ("".equals(awaysumhour)) {
                    awaysumhour = "0";
                }

                String awayendday = Util.null2String((String) mainTableDataMap.get("fhrq"));//出差结束日期
                String awayendtime = Util.null2String((String) mainTableDataMap.get("fhsj"));//出差结束时间
                if (awayendtime.length() > 0) {
                    awayendtime += ":00";
                }

                String awaybeginday = Util.null2String((String) mainTableDataMap.get("ccrq"));//出差日期
                String awaybegintime = Util.null2String((String) mainTableDataMap.get("ccsj"));//出差时间
                if (awaybegintime.length() > 0) {
                    awaybegintime += ":00";
                }

                String awaypsnorder = "0";//出差人员序号
                String awaynote = Util.null2String((String) mainTableDataMap.get("ccsy")).replaceAll("'", "");//出差原因
                awaynote = awaynote.replace("<br>", "");
                awaynote = awaynote.replace("&nbsp;", "");
                String pk_away_b = genEHRPK(requestid);//出差子表主键
                String awayaddress = Util.null2String((String) mainTableDataMap.get("ccmdd")).replaceAll("'", "");//出差目的地
                sql = "insert into tbm_awayb (" +
                        "pk_away_b,awayendtime,awayoffstatus,awaynote,awayaddress," +
                        "awaysumhour,awayendday,awaybegintime,awaybeginday," +
                        "psncode,pk_tbm_psndoc,awaypsnorder,pk_away_h,ts" +
                        ") values (" +
                        "'" + pk_away_b + "','" + awayendtime + "','" + awayoffstatus + "','" + awaynote + "','" + awayaddress + "'," +
                        "'" + awaysumhour + "','" + awayendday + "','" + awaybegintime + "','" + awaybeginday + "'," +
                        "'" + applier + "','" + pk_tbm_psndoc + "','" + awaypsnorder + "','" + pk_away_h + "','" + currenttime + "'" +
                        ")";
                flag = rsds.execute(sql);
            }

            if (!flag) {
                jsonObj.put("status","sql执行fail");
                jsonObj.put("requestid",requestid);
                failcount++;
            } else {
                jsonObj.put("status","success");
                jsonObj.put("requestid",requestid);
                succcount++;
            }
        } catch (Exception e) {
            jsonObj.put("status","同步异常fail");
            jsonObj.put("requestid",requestid);
            failcount++;
        }
       dataList.add(jsonObj);
    }
    jsonData.put("dataList",dataList);
    jsonData.put("总记录数",counts);
    jsonData.put("失败记录数",failcount);
    jsonData.put("成功记录数",succcount);
    out.print(jsonData.toJSONString());
%>
<%!
    public static String genEHRPK(String var0) {
        String var1 = "";
        String var2 = UUID.randomUUID().toString().replaceAll("-", "");
        var1 = "E9" + var0;
        int var3 = 20 - var1.length();
        var1 = var1 + var2.substring(var2.length() - var3);
        return var1;
    }
%>